package fm.douban;

import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class Radio extends UiApplication {
	public static String connectType = ";interface=wifi";
	public static Radio theApp = new Radio();
	public Radio() {
		setupPermissions();
		pushScreen(new WarningScreen());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//Radio theApp = new Radio();
		theApp.enterEventDispatcher();
	}

	public static String getConnectType() {
		// return ";interface=wifi";
		return connectType;
	}

	public static void errorDialog(final String message) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				Dialog.alert(message);
				System.out.println("Error popped out:" + message);
				System.exit(0);
			}
		});

	}

	private void setupPermissions() {
		ApplicationPermissionsManager man = ApplicationPermissionsManager
				.getInstance();
		int[] requiredPerms = new int[] {
				ApplicationPermissions.PERMISSION_WIFI,
				ApplicationPermissions.PERMISSION_INTERNET,
				ApplicationPermissions.PERMISSION_FILE_API };
		ApplicationPermissions perms = man.getApplicationPermissions();
		boolean change = false;
		for (int i = 0; i < requiredPerms.length; i++) {
			if (perms.containsPermissionKey(requiredPerms[i])) {
				if (perms.getPermission(requiredPerms[i]) != ApplicationPermissions.VALUE_ALLOW) {
					change = true;
					perms.addPermission(requiredPerms[i]);
				}
			} else {
				change = true;
				perms.addPermission(requiredPerms[i]);
			}
		}
		if (change) {
			man.invokePermissionsRequest(perms);
		}
	}
}
